package com.yimi.yinhepda.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.LoadTaskEntity;
import com.yimi.yinhepda.entity.bean.BaseDictEntity;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 000432
 */
public class LoadTaskAdapter extends BaseAdapter {
    private List<LoadTaskEntity> datas;
    private LayoutInflater inflater;
    private Activity activity;
    /**
     * 合并装卸车
     */
    private List<BaseDictEntity> mDictCombind;

    public LoadTaskAdapter(Activity activity, List<LoadTaskEntity> datas, Context context) {
        super();
        this.activity = activity;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    /**
     * 获取当前位置信息
     */
    public int getCurrentPosition() {
        //设置其他的不选中
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).isChecked()) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 获取多个跳转的实体集合
     */
    public List<LoadTaskEntity> getCheckJumpEntites() {
        List<LoadTaskEntity> list = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            LoadTaskEntity entity = datas.get(i);
            if (entity.isChecked()) {
                list.add(entity);
            }
        }
        return list;
    }

    @Override
    public int getCount() {
        return null != datas ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_load_task, null);
            holder = new ViewHolder();
            holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linear_top);
            //车标号
            holder.carCode = (TextView) convertView.findViewById(R.id.carCode);
            //下一站
            holder.nextStation = (TextView) convertView.findViewById(R.id.next_station);
            //车牌号
            holder.carNo = ((TextView) convertView.findViewById(R.id.car_number));
            //票数
            holder.totle_ticket = (TextView) convertView.findViewById(R.id.totle_ticket);
            holder.radioButton = (RadioButton) convertView.findViewById(R.id.load_task_radio_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        LoadTaskEntity entity = datas.get(position);

        //配载单号
        if (!entity.getCarCode().startsWith("P")) {
            holder.carCode.setText(entity.getCarMark());
        } else {
            holder.carCode.setText(entity.getCarCode());
        }
        //下一站
        if ((entity.getNextStation()).length() == 0 || "null".equals(entity.getNextStation())) {
            holder.nextStation.setText("");
        } else {
            holder.nextStation.setText(entity.getNextStation());
        }
        if (entity.getCarNo() == null) {
            entity.setCarNo("");
        }
        //车牌号
        if (entity.getCarNo().length() == 0 || "null".equals(entity.getCarNo())) {
            holder.carNo.setText("");
        } else {
            holder.carNo.setText(entity.getCarNo());
        }
        //票数
        if (entity.getPriceNum() == null) {
            holder.totle_ticket.setText("0");
        } else {
            holder.totle_ticket.setText(entity.getPriceNum() + "");
        }

        if (entity.isChecked()) {
            holder.radioButton.setChecked(true);
        } else {
            holder.radioButton.setChecked(false);
        }

        if (entity.isChecked()) {
            holder.linearLayout.setBackgroundColor(activity.getResources().getColor(R.color.green));
        } else {
            holder.linearLayout.setBackgroundColor(activity.getResources().getColor(R.color.white));
        }

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                setCheckPosition(position, holder);
            }
        });

        return convertView;
    }

    /**
     * 设置选择项
     */
    private void setCheckPosition(int position, ViewHolder holder) {
        LoadTaskEntity entity = datas.get(position);
        if (entity.isChecked()) {
            entity.setChecked(false);
            holder.linearLayout.setBackgroundColor(activity.getResources().getColor(R.color.white));
        } else {
            List<LoadTaskEntity> loadTaskEntities = new ArrayList<>();
            int checkItem = 1;
            for (LoadTaskEntity item : datas) {
                if (item.isChecked()) {
                    checkItem++;
                    loadTaskEntities.add(item);
                }
            }
            //是否配置数据字典合并装车的部门
            boolean isComBind = false;
            String currentDeptCode = SharedPreferUtil.getInstance().getDeptCode();
            if (mDictCombind != null) {
                for (BaseDictEntity item : mDictCombind) {
                    //合并装车部门
                    String deptCode = item.getDictValue();
                    //合并装车开关
                    String param1 = item.getExternalParam1();
                    if (currentDeptCode.equals(deptCode) && "1".equals(param1)) {
                        isComBind = true;
                        break;
                    }
                }
            }
            if (checkItem > 10) {
                ToastUtil.showToast("最多选择十个配载单！");
                return;
            }

            //是否为相同下一站
            for (LoadTaskEntity item : loadTaskEntities) {
                if (entity.getNextStationCode().equals(item.getNextStationCode())) {
                    ToastUtil.showToast("不可选择相同下一站的配载单！");
                    return;
                }
            }
            if (!isComBind) {
                if (!loadTaskEntities.isEmpty()) {
                    //是否为相同车牌号
                    boolean sameCarNo = false;
                    for (LoadTaskEntity item : loadTaskEntities) {
                        if (entity.getCarNo().equals(item.getCarNo())) {
                            sameCarNo = true;
                            break;
                        }
                    }
                    if (!sameCarNo) {
                        ToastUtil.showToast("不可选择不同车牌号的配载单！");
                        return;
                    }
                }
            }
            //限制只能选择一个优速快递任务
            if (!entity.getCarCode().startsWith("P")) {
                for (LoadTaskEntity item : loadTaskEntities) {
                    if (!item.getCarCode().startsWith("P")) {
                        ToastUtil.showToast("只能选择一个快递任务！");
                        return;
                    }
                }
            }
            //限制只能选择一个同行装车任务
            if (entity.getTaskType() == 1 && !loadTaskEntities.isEmpty()) {
                ToastUtil.showToast("同行任务不支持合并装车！");
                return;
            } else {
                for (LoadTaskEntity item : loadTaskEntities) {
                    if (item.getTaskType() == 1) {
                        ToastUtil.showToast("同行任务不支持合并装车！");
                        return;
                    }
                }
            }

            entity.setChecked(true);
        }
        notifyDataSetChanged();
    }

    private static class ViewHolder {
        LinearLayout linearLayout;
        TextView carCode;
        TextView nextStation;
        TextView carNo;
        TextView totle_ticket;
        RadioButton radioButton;
    }

    public void setDictCombind(List<BaseDictEntity> dictCombind) {
        this.mDictCombind = dictCombind;
    }
}
